<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test verification using RSA-PSS and salt length of 10.");

jsTestIsAsync = true;

var extractable = true;

// This test data comes from:
// http://csrc.nist.gov/groups/STM/cavp/documents/dss/186-2rsatestvectors.zip (SigVerPSS_186-3.rsp)
var kTestData = {
  "publicKeySpki": "30819D300D06092A864886F70D010101050003818B0030818702818100BE499B5E7F06C83FA0293E31465C8EB6B58AF920BAE52A7B5B9BFEB7AA72DB1264112EB3FD431D31A2A7E50941566929494A0E891ED5613918B4B51B0D1FB97783B26ACF7D0F384CFB35F4D2824F5DD380623A26BF180B63961C619DCDB20CAE406F22F6E276C80A37259490CFEB72C1A71A84F1846D330877BA3E3101EC9C7B020111",
  "hash": "sha-256",
  "message": "c7f5270fca725f9bd19f519a8d7cca3cc5c079024029f3bae510f9b02140fe238908e4f6c18f07a89c687c8684669b1f1db2baf9251a3c829faccb493084e16ec9e28d58868074a5d6221667dd6e528d16fe2c9f3db4cfaf6c4dce8c8439af38ceaaaa9ce2ecae7bc8f4a5a55e3bf96df9cd575c4f9cb327951b8cdfe4087168",
  "saltLength": 10,
  "signature": "11e169f2fd40b07641b9768a2ab19965fb6c27f10fcf0323fcc6d12eb4f1c06b330ddaa1ea504407afa29de9ebe0374fe9d1e7d0ffbd5fc1cf3a3446e4145415d2ab24f789b3464c5c43a256bbc1d692cf7f04801dac5bb401a4a03ab7d5728a860c19e1a4dc797ca542c8203cec2e601eb0c51f567f2eda022b0b9ebddeeefa"
};

var verifyAlgorithm = { name: "RSA-PSS", saltLength: kTestData.saltLength };
var signature = hexStringToUint8Array(kTestData.signature);
var message = hexStringToUint8Array(kTestData.message);

crypto.subtle.importKey("spki", hexStringToUint8Array(kTestData.publicKeySpki), { name: "RSA-PSS", hash: {name: kTestData.hash} }, extractable, ["verify"]).then(function(result) {
    publicKey = result;
    debug("\nVerifying known signature...");
    return crypto.subtle.verify(verifyAlgorithm, publicKey, signature, message);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "true");
    debug("\nVerifying a bad signature...");
    return crypto.subtle.verify(verifyAlgorithm, publicKey, new Uint8Array(256), message);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "false");
}).then(finishJSTest, failAndFinishJSTest);
</script>

</body>
</html>
